﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace QLXuongCNTT
{
    class ConnectDB : DataTable
    {
        private static SqlConnection connection;
        private static SqlDataAdapter adapter = null;
        public static string strconnect = "Data Source=.;AttachDBFilename = " + Directory.GetCurrentDirectory().ToString() + "\\TBVT_KHOACNTT.mdf;Integrated Security=True ";
        private string _error;

        public string Error
        {
            get { return _error; }
            set { _error = value; }
        }


        public ConnectDB()
        {
            Error = "";
            try
            {
                connection = new SqlConnection(strconnect);
                if (connection.State != System.Data.ConnectionState.Open)
                {
                    connection.Open();
                }
            }
            catch (Exception e)
            {
                Error = e.Message;
            }
        }
        public void GetData(SqlCommand command)
        {

            try
            {
                command.Connection = connection;
                adapter = new SqlDataAdapter(command);
                this.Clear();
                adapter.Fill(this);
            }
            catch (Exception e)
            {
                _error = e.Message;
            }
        }
        public int Executive()
        {
            int result = 0;
            try
            {
                SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
                result = adapter.Update(this);
            }

            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                result = 0;
            }
            return result;
        }
        public int Executive(SqlCommand command)
        {
            int result = 0;
            try
            {
                command.Connection = connection;
                result = command.ExecuteNonQuery();
            }

            catch (Exception e)
            {
                _error = e.Message;
            }
            return result;
        }
        public void CloseConnect()
        {
            connection.Close();
        }
    }
}
